let ipts = document.querySelectorAll(".inputs input");
console.log(ipts);
let username = document.querySelector("#username-Tipe");
let buttons = document.querySelector(".button button");
let password = document.querySelector("#password-Tipe");
// let repetition =document.querySelector("#repetition");

let isUsername = false;
ipts[0].onchange = function () {
  let usernameReg = /^[1][3,4,5,7,8][0-9]{9}$/;
  if (usernameReg.test(this.value)) {
    isUsername = true;

    //正则成功发起请求
    let url = `http://jx.xuzhixiang.top/ap/api/checkname.php?username=${this.value}`;
    console.log(url);
    let xhr = new XMLHttpRequest();
    xhr.open("get", url);
    xhr.send();
    xhr.onreadystatechange = function () {
      console.log(1);
      if (xhr.readyState == 4 && xhr.status == 200) {
        console.log("请求成功 结束");
        xhr.responseText;
        console.log(xhr.responseText);
        let obj = JSON.parse(xhr.responseText);
        console.log(obj);

        console.log(username);
        username.innerHTML = obj.msg;
        if (obj.code == 1) {
          // alert(obj.msg)
        } else {
          // alert(obj.msg)
        }
      }
    };
  } else {
    //正则不成功 显示格式不正确
    isUsername = false;
    username.innerHTML = "格式不正确";
  }
};
let isPassword = false;
ipts[1].onchange = function () {
  //  let  =red =/^\S{6,}$/
  let reg = /^\S{6,}$/
  // if (red.test(this.value)) {
  if (reg.test(this.value)) {
    isPassword = true
    password.innerHTML = "格式正确"
  } else {
    isPassword = false
    password.innerHTML = "格式不正确"

  }
}
// let isNOPassword =false
// ipts[2].onchange=function(){
//     if(this.value ==ipts[1].value){
//         isNOPassword =true;
//         repetition.innerHTML ="密码相同"

//     }else{
//         isNOPassword=false;
//         repetition.innerHTML ="密码不同"
//     }
// }
buttons.onclick = function () {
  console.log(isUsername, isPassword);
  if (isUsername && isPassword) {
    let user = ipts[0].value;
    let pass = ipts[1].value;
    let url = `http://jx.xuzhixiang.top/ap/api/reg.php?username=${user}&password=${pass}`;
    let xhr = new XMLHttpRequest()
    xhr.open('get', url)
    xhr.send()
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        let josnStr = xhr.responseText;
        let obj = JSON.parse(josnStr);
        console.log(obj);
        if (obj.code == 1) {
          console.log("注册成功");
          alert(obj.msg)
          location.href = "../html/login.html";
        }
      }
    }
  }
}